package io.bitmax.exchange.login.ui;

import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.OnClick;
import io.bitmax.exchange.R;
import io.bitmax.exchange.core.BaseActivity;
import io.bitmax.exchange.login.present.LoginPresenter;
import io.bitmax.library.utils.NoDoubleClickUtils;
import io.bitmax.library.widget.editor.EditTextHelper;
import io.bitmax.library.widget.editor.SimpleTextWatcher;

/**
 * 用户登录
 * 2018年9月25日
 */
public class LoginActivity extends BaseActivity<LoginPresenter,LoginPresenter.LoginUI>
        implements LoginPresenter.LoginUI {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected LoginPresenter.LoginUI getUI() {
        return this;
    }

    @Override
    public int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        EditTextHelper.getEditTextDeleteIconWrapper(this, etUsername).setTextWatcher(mTextWatcher);
        EditTextHelper.getEditTextDeleteIconWrapper(this, etPassword).setTextWatcher(mTextWatcher);
    }

    @Override
    public void addEvent() {

    }

    @OnClick(R.id.btn_login)
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.btn_login:
            {
                if(!NoDoubleClickUtils.isDoubleClick()){
                    getPresenter().login(etUsername.getText().toString(),etPassword.getText().toString());
                }

                break;
            }
            default:
                break;
        }
    }

    SimpleTextWatcher mTextWatcher = new SimpleTextWatcher(){
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            if(!TextUtils.isEmpty(etPassword.getText().toString().trim())
                    && !TextUtils.isEmpty(etUsername.getText().toString().trim())){
                btnLogin.setEnabled(true);
            }
        }
    };
}
